package controller.uu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.dao.AlbumDaoLocal;
import model.dao.UserDaoLocal;
import model.entity.Album;
import model.entity.Comment;
import model.entity.Photo;
import model.entity.ProfileImage;
import model.entity.User;
import controller.CUtil;

/**
 * Servlet implementation class SAuthorsProfile
 */
public class SAuthorsProfile extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static final int topPictures=10;
    private static final int reacentActivity=15;
	@EJB
	private UserDaoLocal userDao;
	
	@EJB
	private AlbumDaoLocal albumDao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SAuthorsProfile() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int currentUserId=Integer.parseInt((String) request.getParameter("userId"));
		User currentUser=userDao.findById(currentUserId);
		List<Album> albums=albumDao.findAlbumByUser(currentUser.getId());
		ProfileImage profileImage=currentUser.getProfileImage();
		
		List<Photo> photos=new ArrayList<Photo>();
		for(Album a:albums){
			if(!a.getFavorite()){
				for(Photo photo:a.getPhotos()){
					photos.add(photo);
				}
			}
		}
		List<Photo> allPhotos=new ArrayList<Photo>();
		allPhotos.addAll(photos);
		request.setAttribute("allPhotos", allPhotos);
		if(photos.size()!=0){
			Collections.sort(photos, new Comparator<Photo>() {
				public int compare(Photo f1, Photo f2)
	            {
	                return f2.getRating().compareTo(f1.getRating());
	            } 
			});
			int sizeOfList=photos.size();
			request.setAttribute("topK",photos.subList(0, sizeOfList<topPictures?sizeOfList:topPictures));
		}
		
		List<Comment> comments=new ArrayList<Comment>();
		for(Photo p:photos){
			for(Comment c:p.getComment()){
				int cuid=currentUser.getId();
				int ccid=c.getUser().getId();
				if(cuid!=ccid){
					comments.add(c);
				}
			}
		}
		if(comments.size()!=0){
			Collections.sort(comments, new Comparator<Comment>() {
				public int compare(Comment c1, Comment c2)
	            {
	                return c1.getDate().compareTo(c2.getDate());
	            } 
			});
			int sizeOfList=comments.size();
			request.setAttribute("recentActivity",comments.subList(0, sizeOfList<reacentActivity?sizeOfList:reacentActivity));
		}
		
		request.setAttribute("currentUser", currentUser);
		request.setAttribute("albums",albums );
		request.setAttribute("profileImage", profileImage);
		
		User user=(User)request.getSession().getAttribute("user");
		if(user!=null && user.getId().intValue()!=currentUserId){
			boolean contains=false;
			for(User u:user.getFollowing()){
				if(u.getId().intValue()==currentUserId){
					contains=true;
					break;
				}
			}
			if(!contains){
				request.setAttribute("canFollow", new Boolean(true));
			}else{
				request.setAttribute("canFollow", new Boolean(false));
			}
		}
		
		CUtil.redirecter(request, response, "uu/VAuthorsProfile.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
